var token = localStorage.getItem("token");
var searchResult = document.getElementById("searchResult");
var searchNullP = document.getElementById("searchNullP");
var searchData = document.getElementById("searchData");
var list = document.getElementById("list");

var sidList = document.getElementById("sidList");
var warehouseList = document.getElementById("warehouseList");

var pageNum = 1;
var itemNum = 20;
var countNum = 0;

var textStr = "";
var isShishi = "";
var pageFlag = "";
var pageflag = "";
var funName = "";

(function(e) {

	mui.init({
		pullRefresh: {
			container: '#list',
			down: {
				style: 'circle',
				callback: pulldownRefresh
			},
			up: {
				auto: true,
				contentrefresh: '正在加载...',
				callback: pullupRefresh
			}
		}
	});

	setPxPerRem();
	//	changeLineHeight();
	mui.plusReady(function() {
		textStr = getQueryString("textStr");
		isShishi = getQueryString("isShishi");
		pageflag = getQueryString("pageflag");
		if(!isEmpty(textStr)) {
			$("#queryType").html(textStr);
			funName = "bill";
		}

		if(isShishi) {
			funName = funName + "_rt";
			showEle(sidList);
			showEle(warehouseList);
		}

	});
})(mui);

var isRefresh = false;

function pulldownRefresh() {
	removeLi("list");
	isRefresh = true;
	pageNum = 1;
	if(isHaveNet()) {
		getWarehouseIn();
	} else {
		mui.toast("无网络连接！");
	}
}

//上拉加载
var isLoad = false;

function pullupRefresh() {
	isLoad = true;
	if(isHaveNet()) {
		getWarehouseIn();
	} else {
		mui.toast("无网络连接！");
	}
}
var keyword = "";

function getWarehouseIn() {
	token = localStorage.getItem("token");
	if(isEmpty(token)) {
		mui.toast("登录认证失败，请退出后重试");
		return;
	}

	if(isShishi && isGet) {
		if(isEmpty(currentSid) || isEmpty(currentWarehouse)) {
			mui.toast("请选择查询条件");
			return;
		}
	}

	var p = {
		op: funName,
		token: token,
		data: {
			state: "-1",
			keyword: keyword,
			page: pageNum,
			perpage: itemNum
		}
	};

	if(isShishi) {
		p = {
			op: funName,
			token: token,
			data: {
				sid: currentSid,
				type: currentWarehouse,
				keyword: keyword,
				page: pageNum,
				perpage: itemNum
			}
		}
	}

	var jsonData = {
		p: JSON.stringify(p)
	}

	if(checkParams(jsonData)) {
		mui.toast("查询失败！");
		return;
	}

	var opts = {
		url: '',
		addressUrl: "testline",
		requestType: "GET",
		pastDate: jsonData
	}

	crossDomainAjax(opts, function(data) {

		if(!isGet) {
			getSidList();
		} else {
			plus.nativeUI.closeWaiting();
		}
		showEle(searchResult);
		if(!isEmpty(data)) {
			hiddenEle(searchNullP);
			showEle(searchData);
			if(isEmpty(data.list)) {
				$("#searchNull").html(keyword);
				hiddenEle(searchData);
				showEle(searchNullP);
				mui.toast("暂无数据");
			} else {
				var rows = data.list;
				for(var i = 0; i < rows.length; i++) {
					createItem(rows[i], i);
				}
			}

		} else {
			$("#searchNull").html(keyword);
			hiddenEle(searchData);
			showEle(searchNullP);
			mui.toast("暂无数据");
		}

		if(isRefresh) {
			mui('#list').pullRefresh().endPulldown();
			isRefresh = false;
		}

		if(isLoad) {
			isLoad = false;
			mui('#list').pullRefresh().endPullup((pageNum > countNum));
		}
		if(pageNum == 1) {
			mui('#list').pullRefresh().endPullup(false);
		}
		pageNum++;
	});
}
var currentId = "";
var currentCode = "";
var currentType = "";
mui("#list").on("tap", "li", function() {
	var checkBoxs = list.getElementsByTagName("input");
	for(var i = 0; i < checkBoxs.length; i++) {
		checkBoxs[i].checked = false;
	}
	currentType = this.getElementsByTagName("cite")[2].innerHTML;
	var currentBox = this.getElementsByTagName("input")[0];
	currentBox.checked = true;
	currentId = this.id;
	currentCode = this.className;
}); 

$("#seleceInfo").click(function() {
	
	
	if(isEmpty(currentId)) {
		mui.toast("请选择条目后在查看");
		return;
	}
	var postdata = {
		pageflag: "page_password.html",
		itemID: currentId,
		itemCode: currentCode,
		sid:currentSid,
		type: currentType
	}

	var change = {
		pageUrl: "page_billinfo.html",
		postData: postdata,
		aniShow: "fade-in"
	}
	pageChange(change);
});

var insertDiv = document.getElementById("insertDiv");

function createItem(row, index) {
	var createLi = document.createElement("li");
	var ischeck = "";
	var itemId = row.id;
	var billNo = row.bill_no;
	var arap = row.ar_ap;
	var amount = row.amount;
	var order_sn = row.order_sn;
	if(isShishi) {
		itemId = row.BOI_Tid;
		billNo = row.BOI_JobCode;
		arap = row.BOI_Type;
		amount = row.BOI_Money;
		order_sn = currentSid;
	}
	if(index == 0) {
		currentId = itemId;
		currentCode = billNo;
		ischeck = "checked='checked'";
	}
	createLi.id = itemId;
	createLi.className = billNo;
	createLi.innerHTML = '<cite style="width: 1.3rem;">' + itemId + '</cite>' +
		'<cite style="width: 3.8rem;">' + billNo + '</cite>' +
		'<cite style="width: 2.5rem;">' + arap + '</cite>' +
		'<cite style="width: 1.9rem;">¥' + amount + '</cite>' +
		'<cite style="width: 3.8rem;">' + order_sn + '</cite>' +
		'<cite class="mui-checkbox"><input type="checkbox" ' + ischeck + ' name="" id="" value="" /></cite>';
	document.body.appendChild(createLi);
	insertBefore(createLi, insertDiv);
}

$("#queryBtn").click(function() {
	keywords = $("#searchInput").val().trim();
	if(isEmpty(keywords) && !isShishi) {
		mui.toast("请输入账单号查询");
		return;
	}
	pulldownRefresh();
});

var sid = document.getElementById("sid");
var warehouse = document.getElementById("warehouse");
var isGet = false;

function getSidList() {
	token = localStorage.getItem("token");
	var p = {
		op: "provider_list",
		token: token
	}
	var jsonData = {
		p: JSON.stringify(p)
	}

	if(checkParams(jsonData)) {
		mui.toast("查询失败！");
		return;
	}

	var opts = {
		url: '',
		addressUrl: "testline",
		requestType: "GET",
		pastDate: jsonData
	}

	crossDomainAjax(opts, function(data) {
		plus.nativeUI.closeWaiting();
		isGet = true;
		var htmlStr = '<option value="-1" selected>请选择供应商</option>';
		if(!isEmpty(data)) {
			for(var i = 0; i < data.length; i++) {
				htmlStr += '<option value="' + data[i].o_tid + '">' + data[i].o_name + '</option>'
			}
		}
		sid.innerHTML = htmlStr;
	});
}

var currentSid = "";
var currentWarehouse = "";
sid.addEventListener('change', function() {
	currentSid = this.value;
}, false);

warehouse.addEventListener('change', function() {
	currentWarehouse = this.value;
}, false);